const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 将秒数转为时分秒格式
// 传入的必须是秒数才能取整
const secToTime = s => {
  var t = '';
  if (s > -1) {
    var hour = Math.floor(s / 3600);
    var min = Math.floor(s / 60) % 60;
    var sec = s % 60;
    // console.log('hour',hour,'min',min,'sec',sec)
    if (hour > 0){
      console.log('经历')
      if (hour < 10) {
        t = '0' + hour + ":";
      } else {
        t = hour + ":";
      }
    }
    if (min < 10) { t += "0"; }
    t += min + ":";
    if (sec < 10) { t += "0"; }
    t += sec;
  }
  return t;
}


module.exports = {
  formatTime,
  secToTime
}
